package cn.ubest.mvvmcore.utils

import android.os.Process
import android.view.Gravity
import android.widget.Toast
import androidx.annotation.StringRes
import cn.ubest.mvvmcore.BaseApplication

/**
 * Created by maoqi on 2020/9/24.
 */
object ToastUtils {
    /**
     * 全局弹toast
     *
     * @param message
     */
    fun toast(message: String) {
        if (Process.myTid() == BaseApplication.mainThreadId) {
            showToast(message)
        } else {
            BaseApplication.handler
                .post {
                    showToast(message)
                }
        }
    }

    fun toast(@StringRes resId: Int) {
        toast(ResourcesUtils.getString(resId))
    }

    private fun showToast(msg: String) {
        val toast =
            Toast.makeText(BaseApplication.application, msg, Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
    }
}